from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class Tool(db.Model):
    """小工具表"""
    __tablename__ = 'tool'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='工具ID')
    tool_code = db.Column(db.String(32), unique=True, nullable=False, comment='工具代码')
    tool_name = db.Column(db.String(50), nullable=False, comment='工具名称')
    tool_type = db.Column(db.String(20), nullable=False, comment='工具类型：calendar/zodiac/name/dream')
    description = db.Column(db.Text, nullable=True, comment='工具描述')
    icon = db.Column(db.String(255), nullable=True, comment='工具图标')
    status = db.Column(db.SmallInteger, nullable=False, default=1, comment='状态：0-禁用，1-正常')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'id': self.id,
            'tool_code': self.tool_code,
            'tool_name': self.tool_name,
            'tool_type': self.tool_type,
            'description': self.description,
            'icon': self.icon,
            'status': self.status,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class Calendar(db.Model):
    """万年历表"""
    __tablename__ = 'calendar'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='日历ID')
    solar_date = db.Column(db.Date, nullable=False, comment='公历日期')
    lunar_date = db.Column(db.String(20), nullable=False, comment='农历日期')
    ganzhi_year = db.Column(db.String(10), nullable=False, comment='干支年')
    ganzhi_month = db.Column(db.String(10), nullable=False, comment='干支月')
    ganzhi_day = db.Column(db.String(10), nullable=False, comment='干支日')
    zodiac = db.Column(db.String(10), nullable=False, comment='生肖')
    solar_term = db.Column(db.String(10), nullable=True, comment='节气')
    festival = db.Column(db.String(50), nullable=True, comment='节日')
    is_leap_month = db.Column(db.SmallInteger, nullable=False, default=0, comment='是否闰月')
    suitable = db.Column(db.String(255), nullable=True, comment='宜')
    unsuitable = db.Column(db.String(255), nullable=True, comment='忌')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'id': self.id,
            'solar_date': self.solar_date.strftime('%Y-%m-%d'),
            'lunar_date': self.lunar_date,
            'ganzhi_year': self.ganzhi_year,
            'ganzhi_month': self.ganzhi_month,
            'ganzhi_day': self.ganzhi_day,
            'zodiac': self.zodiac,
            'solar_term': self.solar_term,
            'festival': self.festival,
            'is_leap_month': self.is_leap_month,
            'suitable': self.suitable,
            'unsuitable': self.unsuitable,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class Zodiac(db.Model):
    """星座生肖表"""
    __tablename__ = 'zodiac'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='ID')
    zodiac_type = db.Column(db.String(10), nullable=False, comment='类型：constellation/chinese_zodiac')
    zodiac_code = db.Column(db.String(20), nullable=False, comment='代码')
    zodiac_name = db.Column(db.String(20), nullable=False, comment='名称')
    date_range = db.Column(db.String(50), nullable=True, comment='日期范围')
    description = db.Column(db.Text, nullable=True, comment='描述')
    personality = db.Column(db.Text, nullable=True, comment='性格特点')
    love_compatibility = db.Column(db.Text, nullable=True, comment='爱情配对')
    career = db.Column(db.Text, nullable=True, comment='事业')
    fortune = db.Column(db.Text, nullable=True, comment='运势')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'id': self.id,
            'zodiac_type': self.zodiac_type,
            'zodiac_code': self.zodiac_code,
            'zodiac_name': self.zodiac_name,
            'date_range': self.date_range,
            'description': self.description,
            'personality': self.personality,
            'love_compatibility': self.love_compatibility,
            'career': self.career,
            'fortune': self.fortune,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class Dream(db.Model):
    """周公解梦表"""
    __tablename__ = 'dream'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='ID')
    keyword = db.Column(db.String(50), nullable=False, comment='关键词')
    category = db.Column(db.String(50), nullable=False, comment='分类')
    content = db.Column(db.Text, nullable=False, comment='解梦内容')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'id': self.id,
            'keyword': self.keyword,
            'category': self.category,
            'content': self.content,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class NameAnalysis(db.Model):
    """姓名测算表"""
    __tablename__ = 'name_analysis'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='ID')
    name = db.Column(db.String(50), nullable=False, comment='姓名')
    gender = db.Column(db.SmallInteger, nullable=False, comment='性别：1-男，2-女')
    score = db.Column(db.Integer, nullable=False, comment='总分')
    wuxing = db.Column(db.String(50), nullable=False, comment='五行属性')
    fortune = db.Column(db.Text, nullable=True, comment='吉凶分析')
    personality = db.Column(db.Text, nullable=True, comment='性格分析')
    career = db.Column(db.Text, nullable=True, comment='事业分析')
    health = db.Column(db.Text, nullable=True, comment='健康分析')
    suggestion = db.Column(db.Text, nullable=True, comment='建议')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self, include_paid_content=False):
        result = {
            'id': self.id,
            'name': self.name,
            'gender': self.gender,
            'score': self.score,
            'wuxing': self.wuxing,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        
        if include_paid_content:
            result.update({
                'fortune': self.fortune,
                'personality': self.personality,
                'career': self.career,
                'health': self.health,
                'suggestion': self.suggestion
            })
            
        return result
